<?php
/**
 * Created by JetBrains PhpStorm.
 * User: akrug
 * Date: 31.05.12
 * Time: 10:25
 * To change this template use File | Settings | File Templates.
 */
class Model_DbTables_User_User extends Zend_Db_Table_Abstract
{    protected $_name = 'be_users';

    public function init(){
        $this->_setupPrimaryKey();
    }

    /**
     * @return array - all undeleted Users
     */
    public function getUserList(){
        $select = $this->select()
                        ->where('deleted = 0');
        $result = $this->fetchAll($select)->toArray();

        return $result;
    }

    /**
     * @return array - one defined User
     */
    public function getUser($id){
        if(!self::deleted($id)){
            $select = $this->select()->where( $this->_primary[1].' = '.$id);
            $result = $this->fetchAll($select)->toArray();
            return $result[0];
        }

        return false;
    }

    /**
     * @param $id
     * @return bool - returns true if 'deleted'  = 0
     *              - returns false if 'deleted' = 1 OR id not found
     */
    public function deleted($id){
        $select = $this->select()
                        ->where($this->_primary[1].' = '.$id);

        $result = $this->fetchAll($select)->toArray();

        if(empty($result)){
            return true;
        }elseif($result[0]['deleted'] == '1'){
            return true;
        }

        return false;
    }

    public function emailReserved($address){
        $select = $this->select()
            ->where('fe_user_email = ?', $address);

        $result = $this->fetchAll($select)->toArray();

        if(empty($result)){
            return false;
        }

        return true;
    }

    /**
     * @param $id
     * @return bool
     */
    public function deleteUser($id){

        if(!self::deleted($id)){
            $rowsDeleted = $this->update(array('deleted' => 1), $this->_primary[1].' = '.$id);

            if(0 == $rowsDeleted){
                return false;
            }
        }

        return true;
    }

    /**
     * @param $params
     *
     * @return int - inserted id
     */
    public function addUser($params){
        $cols = $this->_getCols();
        foreach($params as $pIndex => $p){
            if(!in_array($pIndex, $cols)){
                unset($params[$pIndex]);
            }
        }

        if(isset($params['fe_user_password'])){
            $params['fe_user_password'] = md5($params['fe_user_password']);
        }

        return $this->insert($params);
    }

    public function isRegistered($id){

        if(!self::deleted($id)){
            $select = $this->select()->where($this->_primary[1].' = '.$id);

            $this->fetchAll($select);

        }
    }

    /**
     * compares each given param index with columnnames
     * if there is no match param will be unset
     *
     * @param $params
     * @return bool
     *
     */
    public function editUser($params){
        $cols    = $this->_getCols();
        $id      = $params['id'];

        if(!$this->deleted($id)){
            foreach($params as $pIndex => $p){
                if(!in_array($pIndex, $cols)){
                    unset($params[$pIndex]);
                }elseif('fe_user_password' == $pIndex){
                    if('' !== $params[$pIndex]){
                        $params[$pIndex] = md5($p);
                    }else{
                        unset($params[$pIndex]);
                    }
                }
            }
            $this->update($params, $this->_primary[1]." = $id");

            return true;
        }

        return false;
    }

    public function passwordValid($id, $password){
        $user = self::getUser($id);

        if($user){
            if(md5($password) == $user['password']){
                return true;
            }
        }

        return false;
    }

    /**
     * @param $id
     * @return bool
     *
     * @purpose locks if locked - unlocks if unlocked
     *
     */
    public function lockUser($id){

        if(!self::deleted($id)){
            $hidden = 1;

            if($this->locked($id)){
                $hidden = 0;
            }

            $rowsDeleted = $this->update(array('locked' => $hidden),  $this->_primary[1].' = '.$id);

            if(0 == $rowsDeleted){
                return false;
            }
        }

        return true;
    }

    /**
     * @param $id
     * @return bool
     *
     * @purpose checks if company is locked
     */
    public function locked($id){
        $company = $this->getUser($id);

        return (bool) $company['locked'];
    }
}
